package com.fenghuajueli.module_user.mine

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import coil.compose.AsyncImage
import com.fenghuajueli.module_user.R
import com.fenghuajueli.module_user.network.result.TemplateResult
import com.fenghuajueli.module_user.ui.theme.ModuleDevTheme

/**
 * @Author: sz_ijj
 * @Date: 2024-01-08 9:32
 **/
class ComposeMineFragment : BaseMineFragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return ComposeView(requireContext()).apply {
            setContent {
                val uiState = viewModel.userUiState.collectAsStateWithLifecycle(lifecycle).value
                val templateResult = viewModel.templateResult.collectAsState(initial = TemplateResult.Default())

                ModuleDevTheme {
                    Surface(
                        modifier = Modifier.fillMaxSize()
                    ) {
                        Column(
                            modifier = Modifier
                                .fillMaxSize()
                                .verticalScroll(rememberScrollState())
                        ) {
                            if (uiState.payStatus) {
                                Column(
                                    modifier = Modifier
                                        .fillMaxWidth()
                                        .padding(top = 20.dp, bottom = 20.dp)
                                        .clickable(
                                            interactionSource = remember {
                                                MutableInteractionSource()
                                            },
                                            indication = null,
                                            onClick = ::toUserInfoContainer
                                        ),
                                    horizontalAlignment = Alignment.CenterHorizontally
                                ) {
                                    AsyncImage(
                                        model = if (uiState.isLocalHead)
                                            R.mipmap.icon_default_avatar
                                        else
                                            uiState.headIcon,
                                        contentDescription = null
                                    )
                                    Text(text = if (uiState.isLogin) viewModel.getUserName() else "登录/注册")
                                }

                                AsyncImage(
                                    modifier = Modifier
                                        .fillMaxWidth()
                                        .clickable(
                                            interactionSource = remember {
                                                MutableInteractionSource()
                                            },
                                            indication = null,
                                            onClick = ::toVipContainer
                                        ),
                                    model = when (val result = templateResult.value) {
                                        is TemplateResult.Success -> {
                                            if (uiState.isVip)
                                                result.data.images.user_pic_member_opened
                                            else
                                                result.data.images.user_pic_member_notopened
                                        }

                                        else -> {
                                            if (uiState.isVip)
                                                R.mipmap.user_mine_membershipcard_02
                                            else
                                                R.mipmap.user_mine_membershipcard_01
                                        }
                                    },
                                    contentDescription = null
                                )

                            } else {
                                Spacer(modifier = Modifier.height(200.dp))
                            }

                            for (mineFeature in mineFeatures) {
                                if (mineFeature.title == "意见反馈") {
                                    MineFeatureWidget(mineFeature, uiState.mDot)
                                } else {
                                    MineFeatureWidget(mineFeature)
                                }
                            }

                            if (uiState.icp.isNotBlank()) {
                                Text(
                                    modifier = Modifier
                                        .fillMaxWidth(),
                                    text = uiState.icp,
                                    color = Color.Gray,
                                    textAlign = TextAlign.Center
                                )
                            }
                        }
                    }
                }
            }
        }
    }

    @Composable
    fun MineFeatureWidget(
        mine: MineFeature,
        mDot: Boolean = false
    ) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 20.dp, vertical = 12.dp)
                .clickable(
                    interactionSource = remember {
                        MutableInteractionSource()
                    },
                    indication = null,
                    onClick = mine.onClick
                )
        ) {
            AsyncImage(model = mine.icon, contentDescription = null)
            Text(text = mine.title)
            if (mDot) {
                Spacer(
                    modifier = Modifier
                        .size(4.dp)
                        .background(Color.Red, shape = RoundedCornerShape(50))
                )
            }
        }
    }

    private val mineFeatures = listOf(
        MineFeature(title = "给个好评", onClick = ::toComment),
        MineFeature(title = "反馈与投诉", onClick = ::toUserFeedBack),
        MineFeature(title = "清理缓存", onClick = ::toClearCache),
        MineFeature(title = "关于我们", onClick = ::toAboutUs),
        MineFeature(title = "联系客服", onClick = ::toContactServer),
        MineFeature(title = "隐私设置", onClick = ::toAllAgreement),
        MineFeature(title = "举报反馈", onClick = ::toReportFeedBack),
        MineFeature(title = "个人信息收集清单", onClick = ::toInfoCollectList),
        MineFeature(title = "第三方共享清单", onClick = ::toThirdSdkList),
    )

    data class MineFeature(
        val icon: Int = com.fenghuajueli.module_user.R.mipmap.user_icon_feedback,
        val title: String = "",
        val onClick: () -> Unit = { },
    )
}